package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.businessException.AddException;
import com.itheima.businessException.DeleteException;
import com.itheima.businessException.FindException;
import com.itheima.businessException.UpdateException;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LiangTianQi
 * @describe 检查项目的service实现类层
 * @date 2020/11/8 17:56
 */
@Service(interfaceClass = CheckItemSevice.class)
public class CheckItemServiceImpl implements CheckItemSevice {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    @Transactional
    public boolean add(CheckItem checkItem) throws AddException{

        try {

            return checkItemDao.add(checkItem);
        }catch (Exception e){
            throw new AddException(MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }

    @Override
    @Transactional
    public boolean delete(Integer id) throws DeleteException{
        try {
            int i=1/0;
            //通过检查项的id查询检查项和检查组中间表的关系，有关系则不删除
            List<Integer> list=checkItemDao.getCheckGroupIDById(id);
            if (list.size() == 0) {
                return checkItemDao.delete(id);
            }else {
                return false;
            }
        }catch (Exception e){
            throw new DeleteException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }

    @Override
    @Transactional
    public boolean update(CheckItem checkItem) throws UpdateException{
        try {

            return checkItemDao.update(checkItem);
        }catch (Exception e){
            throw new UpdateException(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @Override
    public CheckItem get(Integer id) throws FindException{

        try {
//            int i= 1/0;
            return checkItemDao.get(id);
        }catch (Exception e){
             throw new FindException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    @Override
    public PageResult getByConditions(QueryPageBean queryPageBean) throws FindException{
        try {
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            List<CheckItem> checkItems = checkItemDao.getByConditions(queryPageBean.getQueryString());
            PageInfo info = new PageInfo(checkItems);
            PageResult pageResult = new PageResult(info.getTotal(),info.getList());
            return pageResult;
        }catch (Exception e){
            throw new FindException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @Override
    public List<CheckItem> getAll() throws FindException{
        try {
            return checkItemDao.getAll();
        }catch (Exception e){
            throw new FindException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


}
